﻿using System;
using EPiServer.Core;
using EPiServer.PlugIn;

namespace EPiServer.Municipality.Web.UI.PropertyControls {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName = "Google maps")]
    public class PropertyGoogleMapsLocation : PropertyData {

        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region public override Type PropertyValueType
        /// <summary>
        /// Gets the PropertyValueType of the PropertyGoogleMapsLocation
        /// </summary>
        /// <value></value>
        public override Type PropertyValueType {
            get { return typeof(string); }
        }
        #endregion
        #region public override PropertyDataType Type
        /// <summary>
        /// Gets the Type of the PropertyGoogleMapsLocation
        /// </summary>
        /// <value></value>
        public override PropertyDataType Type {
            get { return PropertyDataType.String; }
        }
        #endregion
        #region protected virtual GoogleMapsLocation Location
        /// <summary>
        /// Get/Sets the Location of the PropertyGoogleMapsLocation
        /// </summary>
        /// <value></value>
        protected virtual GoogleMapsLocation Location {
            get {
                return _location;
            }
            set {
                ThrowIfReadOnly();
                if (value == null) {
                    Clear();
                } else if ((_location != value) || IsNull) {
                    _location = value;
                    Modified();
                }
            }
        }
        private GoogleMapsLocation _location;
        #endregion
        #region public override object Value
        /// <summary>
        /// Get/Sets the Value of the PropertyGoogleMapsLocation
        /// </summary>
        /// <value></value>
        public override object Value {
            get {
                return Location;
            }
            set {
                SetPropertyValue(value, delegate {
                    if (value == null)
                        Location = null;
                    else if (value is GoogleMapsLocation)
                        Location = (GoogleMapsLocation)value;
                    else
                        Location = GoogleMapsLocation.Parse(value as string);
                });
            }
        }
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region public override IPropertyControl CreatePropertyControl()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IPropertyControl CreatePropertyControl() {
            return new PropertyGoogleMapsLocationControl();
        }
        #endregion
        #region public static PropertyGoogleMapsLocation Parse(string value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static PropertyGoogleMapsLocation Parse(string value) {
            var property = new PropertyGoogleMapsLocation();
            property.Value = GoogleMapsLocation.Parse(value);

            return property;
        }
        #endregion
        #region public override PropertyData ParseToObject(string value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override PropertyData ParseToObject(string value) {
            return Parse(value);
        }
        #endregion
        #region public override void ParseToSelf(string value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public override void ParseToSelf(string value) {
            Value = Parse(value);
        }
        #endregion
        #region protected override void SetDefaultValue()
        /// <summary>
        /// 
        /// </summary>
        protected override void SetDefaultValue() {
            ThrowIfReadOnly();
            _location = null;
        }
        #endregion
    }
}